package com.ocom.company.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityPageEnum;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.company.PageEnumRequest;
import com.ocom.company.dao.IDaoPageEnum;
import com.ocom.company.service.PageEnumService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PageEnumServiceImpl extends ServiceImpl<IDaoPageEnum, EntityPageEnum> implements PageEnumService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public EntityPageEnum getPageEnum(Integer bind, Integer register) {

		/**
		 * 	如果bind大于0：		select val from page_enum where type =“bindPageUrl”and key = @bind
		 * 	否则如果register大于0：	select val from page_enum where type =“registerPageUrl”and key = @register
		 */
		QueryWrapper<EntityPageEnum> queryWrapper=new QueryWrapper<>();
		if(bind>0){
			queryWrapper.eq("type" ,"bindPageUrl");
			queryWrapper.eq("tip" ,bind);
			queryWrapper.last("limit 1");
			queryWrapper.orderByDesc("update_time");
			return this.getOne(queryWrapper);
		}
		if(register>0){
			queryWrapper.eq("type" ,"registerPageUrl");
			queryWrapper.eq("tip" ,register);
			queryWrapper.last("limit 1");
			queryWrapper.orderByDesc("update_time");
			return this.getOne(queryWrapper);
		}
		return null;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public List<EntityPageEnum> selectPageEnumList(PageEnumRequest request) {
		if(ObjectUtil.isEmpty(request.getModels())){
			return new ArrayList<>();
		}
		return this.list(Wrappers.lambdaQuery(EntityPageEnum.class)
				.likeRight(EntityPageEnum::getType,request.getType())
				.in(EntityPageEnum::getTip,request.getModels())
		);
	}
}
